package com.idbk.chargestation.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.idbk.chargestation.R;

import java.util.Arrays;
import java.util.List;

/**
 * 铁匠版本 下拉选项 适配器
 * @author lupc
 *
 */
public class TJDropdownAdapter extends BaseAdapter {

	private LayoutInflater mInflater;

	private List<String> mData;

	private int mSelectedIndex = 0;

	public TJDropdownAdapter(Context context, String[] data){
		mInflater = LayoutInflater.from(context);
		mData = Arrays.asList(data);
	}

	public void setSelectedItemIndex(int index){
		mSelectedIndex = index;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v;
		Hold hold;
		if(convertView == null){
			v = mInflater.inflate(
					R.layout.listview_for_tj_dropdown_item,
					parent, 
					false);
			hold = new Hold();
			hold.mParent = v;
			hold.mViewCheckedLine = v.findViewById(R.id.view_checked_line);
			hold.mTextContent = (TextView)v.findViewById(R.id.textview_content);
			hold.mImageCheckedBox = (ImageView)v.findViewById(R.id.imageview_checkbox);

			v.setTag(hold);
		} else {
			v = convertView;
			hold = (Hold)v.getTag();
		}

		bindView(position, hold);

		return v;
	}

	private void bindView(int position,Hold hold){
		hold.mTextContent.setText(Html.fromHtml(mData.get(position)));

		if(position == mSelectedIndex){
			hold.mParent.setBackgroundColor(Color.rgb(232, 232, 232));
			hold.mViewCheckedLine.setVisibility(View.VISIBLE);
			hold.mImageCheckedBox.setVisibility(View.VISIBLE);
		} else {
			hold.mParent.setBackgroundResource(R.drawable.tj_listview_item_selector);
			hold.mViewCheckedLine.setVisibility(View.INVISIBLE);
			hold.mImageCheckedBox.setVisibility(View.INVISIBLE);
		}
	}

	private class Hold {
		private View mParent;
		private View mViewCheckedLine;
		private TextView mTextContent;
		private ImageView mImageCheckedBox;
	}

}